/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       buy_building_detail.h

	$Header: /game/buy_building_detail.h $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "buy_building_detail.h"

#include "adventure_map.h"
#include "bitmap_group_cache.h"
#include "bitmap_layer_window.h"
#include "bound_handler.h"
#include "building_traits.h"
#include "button.h"
#include "button_cache.h"
#include "external_string.h"
#include "format_string.h"
#include "interface_help.h"
#include "item_help_block.h"
#include "material_icons.h"
#include "replace_keywords.h"
#include "standard_fonts.h"
#include "text_window.h"
#include "town.h"

static t_bitmap_group_cache  g_layout( "dialog.buy_building_detail" );
extern t_bitmap_group_cache* g_building_thumbnails[];
extern t_button_cache        g_cancel_button;

extern t_external_string const k_hotkey_buy;
extern t_button_cache const  k_buy_button("button.buy" );
static t_external_string const k_text_buy_building_title ( "buy_building.title" );
static t_external_string const k_text_built_building ( "built_building.dialog" );
static t_external_string const k_text_not_enough_resources ( "not_enough_resources_to_build.dialog" );
static t_external_string const k_text_built_for_day ( "already_built_for_day.dialog" );
extern t_external_string const k_buy_building_not_owner;

// ------------------------------------------------
// detail dialog for purchasing a building
// ------------------------------------------------
t_buy_building_detail::t_buy_building_detail( t_town* town, t_town_building building, 
											  t_window* parent )
					 : t_window( k_no_transparency, parent )
{
	t_bitmap_group_ptr layout = g_layout.get();
	t_screen_rect window_size = layout->find( "background" )->get_rect();
	t_help_block const& help = get_help_block( "town_hall" );

	t_window*             background;
	t_bitmap_layer const* background_layer;

	background_layer = layout->find( "background" );
	background = new t_bitmap_layer_window( background_layer, t_screen_point(0,0), this );
	m_layout = g_layout.get();

	// add title
	t_bitmap_layer const* layer = layout->find( "title" );
	t_screen_rect         rect = layer->get_rect();
	t_text_window*        text_window;
	std::string           text;
	t_building_traits const& traits = get_traits( town->get_type(), building );

    text = k_text_buy_building_title;
	text_window = new t_text_window( get_font( rect.height() ), rect, this,
		                             text, t_pixel_24(0,0,0) );
	text_window->set_center_horizontal();
	text_window->set_center_vertical();
	text_window->set_drop_shadow( true, t_pixel_24(200,200,200) );

	// add thumbnail
	t_window*               window;
	t_bitmap_layer const*   thumbnail;
    t_screen_point          thumbnail_offset;

	m_thumbnails = g_building_thumbnails[town->get_type()]->get();
	m_materials = g_material_icons.get();
	layer = layout->find( "thumbnail" );
	thumbnail = m_thumbnails->find( traits.keyword );
	
    thumbnail_offset = t_screen_point( (layer->get_width() - thumbnail->get_width()) / 2, 
		                           (layer->get_height() - thumbnail->get_height()) / 2 );
	window = new t_bitmap_layer_window( thumbnail, layer->get_rect().top_left() + thumbnail_offset, this );

	// add name.
	rect = layout->find( "structure_name" )->get_rect();
	text_window = new t_text_window( get_font( 14 ), rect, this,
		                             traits.name.c_str(), t_pixel_24( 0, 0, 0 ) );
	text_window->set_center_horizontal();
	text_window->set_center_vertical();
	text_window->set_drop_shadow( true, t_pixel_24(200,200,200) );

	// add description
	layer = layout->find( "description" );
	rect = layer->get_rect();
	text_window = new t_text_window( get_font( 18 ), rect, this,
		                             traits.help_text, t_pixel_24( 0, 0, 0 ) );
	text_window->set_center_horizontal();
	text_window->set_center_vertical();
	text_window->set_drop_shadow( true, t_pixel_24(200,200,200) );

	t_adventure_map & map = *town->get_map();
	int             player_number = map.get_player_number();

	// add requirements
	layer = layout->find( "requirements" );
	if (town->has( building ))
		text = replace_keywords( k_text_built_building, "%building", traits.name.c_str() );
	else if ( town->get_owner_number() != player_number )
		text = k_buy_building_not_owner;
	else if (!town->can_build( building ))
		text = town->get_requirements_text( building );
	else if (!town->can_afford( building ))
		text = k_text_not_enough_resources;
	else if (town->has_built())
	    text = k_text_built_for_day;
	else
		text = town->get_requirements_text( building );

	text_window = new t_text_window( get_font( 19 ), layer->get_rect(), this,
		                             text, t_pixel_24(0,0,0) );
	text_window->set_center_horizontal();
	text_window->set_center_vertical();
	text_window->set_drop_shadow( true, t_pixel_24(200,200,200) );

	// add buy button
	t_button* button;

	if (!town->has( building ) && town->can_build( building ) && town->can_buy( building ) && town->get_owner_number() == player_number)
	{
		layer = layout->find( "buy_button" );
		button = new t_button( k_buy_button.get(), layer->get_rect().top_left(), this );
		button->set_click_handler( bound_handler( *this, &t_buy_building_detail::buy ) );
		set_help( button, help, "buy" );
	}

	// add cancel button
	layer = layout->find( "cancel_button" );
	button = new t_button( g_cancel_button.get(), layer->get_rect().top_left(), this );
	button->set_click_handler( bound_handler( *this, &t_buy_building_detail::cancel ) );
	set_help( button, help, "cancel" );

	create_material_icons( traits.cost );

	// initialize the dialog
	window_size += t_screen_point( (parent->get_width() - window_size.width()) / 2, 
		                           (parent->get_height() - window_size.height()) / 2 );
	init( window_size );
}
 
// ------------------------------------------------
// detail dialog for purchasing a building
// ------------------------------------------------
void t_buy_building_detail::buy( t_button* )
{
	m_return_value = 1;
	close();
}

// ------------------------------------------------
// detail dialog for purchasing a building
// ------------------------------------------------
void t_buy_building_detail::cancel( t_button* )
{
	m_return_value = 0;
	close();
}

// ------------------------------------------------
// detail dialog for purchasing a building
// ------------------------------------------------
void t_buy_building_detail::create_material_icons( t_material_array const& cost )
{
	int count = 0;
	int i;

	for (i = 0; i < k_material_count; i++)
		if (cost[i] > 0)
			count++;

	if (count == 0)
		return;

    int             material_count = 0;
	t_window*       window;
    t_screen_rect   material_rect;
    t_screen_rect   resource_rect;
    t_screen_rect   text_rect;
    t_screen_point  offset;
	t_text_window*  text_window;
	std::string     text;
	t_help_block const&   material_help = get_help_block( "material_display" );

    // Find offset.
    resource_rect = m_layout->find( "resource_1" )->get_rect();
	material_rect = m_materials->find( k_material_keyword[0] )->get_rect();
	offset = t_screen_point( (resource_rect.width() - material_rect.width()) / 2, 
		                           (resource_rect.height() - material_rect.height()) / 2 );

	for (i = 0; i < k_material_count; i++)
    {
		if (cost[i] <= 0)
            continue;
		
        resource_rect = m_layout->find( format_string( "resource_%i", material_count + 1 ) )->get_rect();
		window = new t_bitmap_layer_window( m_materials->find( k_material_keyword[i] ),
				                                resource_rect.top_left() + offset, this );
		set_help( window, material_help, k_material_keyword[i] );
        
		text = format_string( "%i", cost[i] );
        text_rect = m_layout->find( format_string( "resource_%i_text", material_count + 1 ) )->get_rect();
		text_window = new t_text_window( get_font( text_rect.height() ), text_rect, this, text, t_pixel_24(0,0,0) );
		text_window->set_center_horizontal();
        material_count++;
    }
}
